package hsh.gui;

import hsh.gui.comodo.Comodo;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class RoomModelPanel extends JPanel {

	private Image image;

	private Image image2;

	private List<Image> trashes;

	private Image fire;

	//private Comodo room = null;
	
	private Point[] points = { new Point(10, 10), new Point(60, 60),
			new Point(110, 110), new Point(160, 160) };

	private Point[] fireLocations = { new Point(10, 160), new Point(160, 10),
			new Point(300, 350) };

	private int count = 0;

	private boolean incedio = false;

	public RoomModelPanel(String comodo,String lixo) {
		this.image = this.getImage(comodo);

		this.image2 = this.getImage(lixo);

		this.fire = this.getImage("resources/icons/fire_teste2.png");

		this.initialize();
	}

	protected void initialize() {
		/*this.image = this.getImage("resources/house/dinning/dinning.png");

		this.image2 = this.getImage("resources/house/etc/jornais.png");

		this.fire = this.getImage("resources/icons/fire_teste2.png");
*/
		this.trashes = new ArrayList<Image>();
		for (int i = 0; i < 4; i++) {
			this.trashes.add(image2);
		}

		this.setLayout(new BorderLayout());
	}

	public Image getImage(String path) {
		// URL imageURL = getClass().getResource(path);
		// if (imageURL == null)
		// return null;

		return new ImageIcon(path).getImage();
	}

	public void setImage(Image i) {
		// URL imageURL = getClass().getResource(path);
		// if (imageURL == null)
		// return null;

		this.image = i;
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Dimension dDesktop = this.getSize();

		double width = dDesktop.getWidth();
		double height = dDesktop.getHeight();

		Image background = new ImageIcon(this.image.getScaledInstance(
				(int) width, (int) height, 1)).getImage();

		Image t = new ImageIcon(this.image2.getScaledInstance(75, 50, 1))
				.getImage();

		g.drawImage(background, 0, 0, this);

		for (int i = 0; i < count; i++) {

			g.drawImage(t, points[i].x, points[i].y, this);
		}

		if (this.incedio) {
			g.drawImage(fire, fireLocations[0].x, fireLocations[0].y, this);
			g.drawImage(fire, fireLocations[1].x, fireLocations[1].y, this);
			g.drawImage(fire, fireLocations[2].x, fireLocations[2].y, this);
		}

	}

	public void addTrash() throws InterruptedException {
		for (int i = 0; i < trashes.size(); i++) {

			count++;
			Thread.sleep(1000);

			repaint();
		}
	}

	public void removeTrash() throws InterruptedException {

		count--;
		Thread.sleep(1000);
		repaint();
	}

	public void initFire() throws InterruptedException {

		this.incedio = true;

		Thread.sleep(1000);
		repaint();
	}

	public void extinguishFire() throws InterruptedException {
		this.incedio = true;

		Thread.sleep(1000);
		repaint();
	}

	/*public static void main(String[] args) throws InterruptedException {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		RoomModelPanel p = new RoomModelPanel();
		frame.setContentPane(p);
		frame.setSize(400, 300);
		frame.setVisible(true);
		p.addTrash();
		p.removeTrash();
		p.addTrash();
		p.removeTrash();
		p.addTrash();
		p.removeTrash();

		// frame.repaint();
	}*/
}
